//
//  DCLeixingCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/7.
//  Copyright © 2016年 game. All rights reserved.
//

#import "DCLeixingCtrl.h"
#import "SelectNavView.h"
#import "DCLeixingCellCell.h"
#import "IslanDistance_typeModel.h"
#import "CGFdbmBase.h"
#import "PageModels.h"
#import "ValueModel.h"
@interface DCLeixingCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) SelectNavView *naV;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSArray *titleAry;
@property (nonatomic,strong) NSMutableArray *selctAry;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) IslanDistance_typeModel *typeModel;

@property (nonatomic,copy) NSString *resultString;
@property (nonatomic,copy) NSString *idString;
@property (nonatomic,strong) NSMutableArray *IdAry;

@end

@implementation DCLeixingCtrl

-(NSMutableArray *)IdAry{

    if (!_IdAry) {
        _IdAry = [NSMutableArray array];
    }
    return _IdAry;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray *)selctAry{
    if (!_selctAry) {
        _selctAry = [NSMutableArray array];
    }
    return _selctAry;
}
-(NSArray *)titleAry{
    if (!_titleAry) {
        _titleAry = @[@"不限",@"黑坑",@"斤塘",@"农家乐",@"湖库",@"江河",@"野塘"];
    }
    return _titleAry;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    [self CreatNavView];
}
-(void)CreatNavView{
    self.naV = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.naV.titile.text = @"选择钓场类型";
    self.naV.share.titleLabel.font = font(SCREEN_WIDTH*0.05);
    [self.naV.share setTitle:@"确定" forState:UIControlStateNormal];
    self.naV.share.y = 21;
    self.naV.share.x = SCREEN_WIDTH - 60;
    [self.view addSubview:self.naV];
    [self.naV.bacBtn addTarget:self action:@selector(Back) forControlEvents:UIControlEventTouchUpInside];
    [self.naV.share addTarget:self action:@selector(Tijiao) forControlEvents:UIControlEventTouchUpInside];
}
-(void)Back{
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }

}
-(void)Tijiao{
    
    if (_DCLeixingBlock) {
        _DCLeixingBlock(self.resultString,self.idString);
    }
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    [self DataFromSQL];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    [_tableview registerNib:[UINib nibWithNibName:@"DCLeixingCellCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
    
    _tableview.tableFooterView = [UIView new];
}

-(void)DataFromSQL{
    [CGFdbmBase OpenSQlWhiPathName:@"FirstPage.db"];
    NSInteger TableCount = [CGFdbmBase getTableItemCount:@"FirstPage"];
    
    if (TableCount!=0) {
        [self.dataArray addObjectsFromArray:[CGFdbmBase ListWhith:@"FirstPage" dicName:@"Dic"]];
        PageModels *pageModel = [[PageModels alloc]initWithDictionary:_dataArray.firstObject error:nil];
        self.typeModel = pageModel.diaochang.filt_type[0];
    }
    [_tableview reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.typeModel.value.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = 0;
    DCLeixingCellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    
    ValueModel *model = self.typeModel.value[indexPath.row];
    cell.text.text = model.name;
        cell.resultString = self.sssst;
    
    cell.cellBtnClick = ^{//cell右边 打钩的Btn点击事件
        [self tableView:tableView didSelectRowAtIndexPath:indexPath];
        
    };

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return SCREEN_HEIGHT*0.08;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    DCLeixingCellCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.Select = !cell.Select;
    
    ValueModel *model = self.typeModel.value[indexPath.row];
    
    [self selctArySetWhithModel:model IsSelect:cell.Select];
}

-(void)selctArySetWhithModel:(ValueModel *)valueModel IsSelect:(BOOL)Select{

    if (Select) {
        
            [self.selctAry addObject:valueModel];
    
    }else{
    
        NSInteger index =0 ;
        
        for (ValueModel *model in self.selctAry) {
            
            if ([valueModel.name isEqualToString:model.name]) {
         
                index = [self.selctAry indexOfObject:model];
            }
        }

        [self.selctAry removeObjectAtIndex:index];
    
    }
    
    self.resultString = @"";
    self.idString = @"";
    
    for (int i=0;i<self.selctAry.count; i++) {
        ValueModel *model = self.selctAry[i];
        if (i==0) {
            
             self.resultString = model.name;
            self.idString = model.Id;
            
        }
        
        if (i>0) {
            self.idString = [self.idString stringByAppendingString:[NSString stringWithFormat:@",%@",model.Id]];
            self.resultString = [self.resultString stringByAppendingString:[NSString stringWithFormat:@"  %@",model.name]];
        }
    }
}
@end
